/*
 * listtest.c
 *
 *  Created on: 2012-7-9
 *      Author: rendong
 */
#ifndef CHAT_LISTTEST_H_
#define CHAT_LISTTEST_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../core/chat_list.h"

void test_list (int n)
{
	int i;
	char *value;
	char *tmp;

	printf("test_list n=%d\n", n);

	chat_list_t *list = chat_list_create();
	char *values[n];
	for(i=0; i< n; i++){
		values[i] = malloc(100);
		sprintf(values[i], "value_%d", i);
	}

	// push shift test
	for(i=0; i< n; i++){
		value = values[i];
		chat_list_push(list, value);
	}

	for(i=0; i< n; i++){
		value = values[i];

		tmp = chat_list_shift(list);
		if (tmp == NULL||strcmp(value, tmp) != 0){
			printf("push shift test fail value:%s, get value:%s\n", value, tmp);
			exit(EXIT_FAILURE);
		}
	}
	printf("push shift test success!");
	printf(" list size:%zd head:%p, last:%p\n", list->size, list->head, list->last);


	// push pop test
	for(i=0; i< n; i++){
		value = values[i];
		chat_list_push(list, value);
	}

	for(i=n-1; i>-1; i--){
		value = values[i];

		tmp = chat_list_pop(list);
		if (tmp == NULL||strcmp(tmp, value) != 0){
			printf("push pop test fail value:%s, get value:%s\n", value, tmp);
			exit(EXIT_FAILURE);
		}
	}
	printf("push pop test success!");
	printf(" list size:%zd head:%p, last:%p\n", list->size, list->head, list->last);



	// unshift shift test
	for(i=0; i< n; i++){
		value = values[i];
		chat_list_unshift(list, value);
	}

	for(i=n-1; i>-1; i--){
		value = values[i];

		tmp = chat_list_shift(list);
		if (tmp == NULL|| strcmp(tmp, value) != 0){
			printf("unshift shift test fail value:%s, get value:%s\n", value, tmp);
			exit(EXIT_FAILURE);
		}
	}
	printf("unshift shift test success!");
	printf(" list size:%zd head:%p, last:%p\n", list->size, list->head, list->last);




	// unshift pop test
	for(i=0; i< n; i++){
		value = values[i];
		chat_list_unshift(list, value);
	}

	for(i=0; i< n; i++){
		value = values[i];

		tmp = chat_list_pop(list);
		if (tmp == NULL|| strcmp(tmp, value) != 0){
			printf("unshift pop test fail value:%s, get value:%s\n", value, tmp);
			exit(EXIT_FAILURE);
		}
	}
	printf("unshift pop test success!");
	printf(" list size:%zd head:%p, last:%p\n", list->size, list->head, list->last);


	// push del test
	for(i=0; i< n; i++){
		value = values[i];
		chat_list_push(list, value);
	}

	for(i = 0; i< n; i++){
		value = values[i];
		chat_list_del(list, value);
	}

	printf("push del test success!");
	printf(" list size:%zd head:%p, last:%p\n", list->size, list->head, list->last);

	free(list);
	for(i = 0; i< n; i++){
		value = values[i];
		free(value);
	}
}


#endif
